using B.S.XZYData.Api.Wrter.Application.Command.WorkReportDefectiveItem;
using B.S.XZYData.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

namespace B.S.XZYData.Api.Wrter.Controllers
{
    /// <summary>
    /// 报工-不良品项子表写入接口
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class WorkReportDefectiveItemController : ControllerBase
    {
        private readonly IMediator _mediator;

        public WorkReportDefectiveItemController(IMediator mediator)
        {
            _mediator = mediator;
        }

        /// <summary>
        /// 导入报工-不良品项（批量导入）
        /// </summary>
        [HttpPost]
        public Task<ApiResult<int>> Import([FromBody] ImportWorkReportDefectiveItemsCommand request)
        {
            return _mediator.Send(request);
        }

        /// <summary>
        /// 根据报工ID导出不良品项子表（无样式 Excel）
        /// </summary>
        [HttpGet]
        public async Task<FileContentResult> Export([FromQuery] ExportWorkReportDefectiveItemsQuery query)
        {
            var bytes = await _mediator.Send(query);
            var fileName = $"报工_{query.WorkReportId}_不良品项.xlsx";
            return File(bytes, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", fileName);
        }
    }
} 